package cn.terys.terystransactionclient;

import cn.emagsoftware.ui.dialog.DialogManager;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.terys.terystransactionclient.data.NetManager;
import cn.terys.terystransactionclient.data.SPManager;
import cn.terys.terystransactionclient.data.User;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public abstract class BaseActivity extends Activity
{

    private LinearLayout mTitleWrap = null;
    private TextView     mTitle     = null;
    private LinearLayout mContent   = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setContentView(R.layout.base);
        mTitleWrap = (LinearLayout) super.findViewById(R.id.titleWrap);
        mTitle = (TextView) super.findViewById(R.id.title);
        TextPaint tp = mTitle.getPaint();
        tp.setFakeBoldText(true);
        mContent = (LinearLayout) super.findViewById(R.id.contentWrap);
    }

    @Override
    public void setTitle(CharSequence title)
    {
        // TODO Auto-generated method stub
        super.setTitle(title);
        if (mTitle != null)
            mTitle.setText(title);
    }

    @Override
    public void setTitle(int titleId)
    {
        // TODO Auto-generated method stub
        super.setTitle(titleId);
        if (mTitle != null)
            mTitle.setText(titleId);
    }

    @Override
    public void setTitleColor(int textColor)
    {
        // TODO Auto-generated method stub
        super.setTitleColor(textColor);
        if (mTitle != null)
            mTitle.setTextColor(textColor);
    }

    public void setTitleContent(View view)
    {
        mTitleWrap.removeAllViews();
        mTitle = null;
        mTitleWrap.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
    }

    @Override
    public void setContentView(int layoutResID)
    {
        // TODO Auto-generated method stub
        mContent.removeAllViews();
        View view = getLayoutInflater().inflate(layoutResID, null);
        mContent.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
    }

    @Override
    public void setContentView(View view)
    {
        // TODO Auto-generated method stub
        mContent.removeAllViews();
        mContent.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
    }

    @Override
    public void setContentView(View view, LayoutParams params)
    {
        // TODO Auto-generated method stub
        mContent.removeAllViews();
        mContent.addView(view, params);
    }

    @Override
    public void addContentView(View view, LayoutParams params)
    {
        // TODO Auto-generated method stub
        mContent.addView(view, params);
    }

    @Override
    public View findViewById(int id)
    {
        // TODO Auto-generated method stub
        return mContent.findViewById(id);
    }

    public void quit()
    {
        if (MainActivity.USER != null && MainActivity.USER.role == User.ROLE_OPERATOR)
        {
            DialogManager.showAlertDialog(this, R.string.base_quitdialog_title, R.string.base_quitdialog_msg_signoff, new int[] { R.string.generic_dialog_btn_yes, R.string.generic_dialog_btn_no,
                    R.string.generic_dialog_btn_cancel }, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    // TODO Auto-generated method stub
                    if (which == DialogInterface.BUTTON_POSITIVE)
                    {
                        new GenericAsyncTask<Object, Object, Object>(BaseActivity.this)
                        {
                            @Override
                            protected Object doInBackgroundImpl(Object... arg0) throws Exception
                            {
                                // TODO Auto-generated method stub
                                NetManager.signOff();
                                return null;
                            }

                            protected void onPostExecute(Object[] objs, Object result)
                            {
                                // TODO Auto-generated method stub
                                super.onPostExecute(objs, result);
                                Context context = (Context) objs[0];
                                SPManager.clearUserSignedTag(context);
                                StaticManager.sendQuitIntentToRootActivity(context, WelcomeActivity.class);
                            }
                        }.execute("");
                    } else if (which == DialogInterface.BUTTON_NEUTRAL)
                    {
                        StaticManager.sendQuitIntentToRootActivity(BaseActivity.this, WelcomeActivity.class);
                    }
                }
            }, true, false);
        } else
        {
            DialogManager.showAlertDialog(this, R.string.base_quitdialog_title, R.string.base_quitdialog_msg, new int[] { R.string.generic_dialog_btn_ok, R.string.generic_dialog_btn_cancel },
                    new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // TODO Auto-generated method stub
                            if (which == DialogInterface.BUTTON_POSITIVE)
                            {
                                StaticManager.sendQuitIntentToRootActivity(BaseActivity.this, WelcomeActivity.class);
                            }
                        }
                    }, true, false);
        }
    }

}
